import { reqAddressList, delAddressApi } from '../../../api/address'
import { swiperCellBehavior } from '@/behaviors/swiperCell'
const app = getApp()
Page({
  behaviors: [swiperCellBehavior],
  // 页面的初始数据
  data: {
    addressList: []
  },

  // 去编辑页面
  toEdit(event) {
    const { id } = event.currentTarget.dataset
    wx.navigateTo({
      url: `/modules/settingModule/pages/address/add/index?id=${id}`
    })
  },
  async delAddress(event) {
    // console.log(event)
    const { id } = event.currentTarget.dataset
    const res = await delAddressApi(id)
    console.log(res)
    this.getAddressList()
  },
  async getAddressList() {
    const res = await reqAddressList()
    // console.log(res)
    this.setData({
      addressList: res.data
    })
  },
  changeAddress(e) {
    if (this.flag != '1') return
    const addressId = e.currentTarget.dataset.id
    // console.log(addressId)
    const selectAddress = this.data.addressList.find(
      (item) => item.id === addressId
    )
    if (selectAddress) {
      app.globalData.address = selectAddress
      wx.navigateBack()
    }
  },
  onShow() {
    this.getAddressList()
  },
  onLoad(options) {
    this.flag = options.flag
  }
})
